from rest_framework import serializers

from .models import ResumeChatMessage, ResumeChatSession


# from coze_resume_agent.models import ChatSession, ChatMessage

class ResumeChatSerializer(serializers.Serializer):
    """简历助手聊天请求序列化器"""
    prompt = serializers.CharField(
        required=True,
        min_length=1,
        error_messages={"required": "请输入需求", "min_length": "需求不能为空"}
    )
    user_id = serializers.CharField(required=False, default="123456789")  # 可选，默认用户ID
    filename = serializers.CharField(required=False, allow_blank=True)
    resume_text = serializers.CharField(required=False, allow_blank=True)
    image_url = serializers.URLField(required=False, allow_blank=True)  # 添加图片URL字段
    # file_id = serializers.CharField(required=False, allow_blank=True)  # 接收文件上传组件返回的file_id


# 文件序列化
class ResumeUploadSerializer(serializers.Serializer):
    file = serializers.FileField() # 要求前端传递的字段名必须是"file"


# 消息序列化器（嵌套在会话中）
class ResumeChatMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResumeChatMessage
        fields = ['id', 'sender', 'content', 'created_at', 'tokens_used']
        read_only_fields = fields  # 所有字段都是只读的


# 会话序列化器（包含消息列表）
class ResumeChatSessionSerializer(serializers.ModelSerializer):
    messages = ResumeChatMessageSerializer(many=True, read_only=True)  # 嵌套消息列表

    class Meta:
        model = ResumeChatSession
        fields = ['id', 'title', 'resume_filename', 'resume_text', 'created_at', 'updated_at', 'is_archived', 'messages']
        read_only_fields = fields

